﻿using System;
using Microsoft.SharePoint;

namespace SIMSProject.Helpers
{
    public class UserFieldCreator : BaseFieldCreator
    {
        public UserFieldCreator(string name) : base(name)
        {
        }

        public override string ValidationFormula
        {
            get { throw new NotSupportedException(); }
            set { throw new NotSupportedException(); }
        }

        public SPFieldUserSelectionMode SelectionMode { get; set; }
        public bool AllowMultipleValues { get; set; }
        public int SelectionGroup { get; set; }

        internal override void CreateField(SPList list)
        {
            var name = list.Fields.Add(Name, SPFieldType.User, Required);
            var field = (SPFieldUser) list.Fields.GetFieldByInternalName(name);
            field.Description = Description;
            field.AllowMultipleValues = AllowMultipleValues;
            field.SelectionMode = SelectionMode;
            field.SelectionGroup = SelectionGroup;

            if (EnforceUniqueValues)
            {
                field.Indexed = true;
                field.EnforceUniqueValues = true;
            }

            field.Update();
        }
    }
}